<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<!--质检合格模态框-->
<div class="modal fade" id="modal-success">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">合格</h4>
            </div>
            <div class="modal-body">
                <form id="successForm" class="form-horizontal" role="form">
                    <input type="hidden" id="sucSheetId" name="id">
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">类型</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="sucStatus" name="status">
                                <option value="1" selected="selected">合格</option>
                                <option value="2">让步放行</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">质检结果</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="result"
                                   placeholder="请输入质检结果">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-sucess">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<!--质检驳回模态框-->
<div class="modal fade" id="modal-reject">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">驳回</h4>
            </div>
            <div class="modal-body">
                <form id="rejectForm" class="form-horizontal" role="form">
                    <input type="hidden" id="rejSheetId" name="id">
                    <input type="hidden" name="status" value="4">
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">驳回原因</label>
                        <div class="col-sm-10">
                            <textarea type="text" class="form-control" name="reason"
                                      placeholder="请输入驳回原因"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-reject">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<!--质检报废模态框-->
<div class="modal fade" id="modal-scrap">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">报废</h4>
            </div>
            <div class="modal-body">
                <form id="scrapForm" class="form-horizontal" role="form">
                    <input type="hidden" id="scrSheetId" name="id">
                    <input type="hidden" name="status" value="5">
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">报废原因</label>
                        <div class="col-sm-10">
                            <textarea type="text" class="form-control" name="reason"
                                      placeholder="请输入报废原因"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-scrap">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<section class="content-header">
    <h1>派工单-质检检验</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12" style="margin-top: 50px;margin-left: 2%;margin-right: 2%;">
            <div class="form-inline">
                <div class="form-group" style="margin-right: 50px;">
                    <label>用户名：[[${user.name}]]</label>
                </div>
                <div class="form-group" style="margin-right: 50px;">
                    <label>部门：[[${user.deptName}]]</label>
                </div>
                <div class="form-group" style="margin-right: 50px;">
                    <label>车间：[[${user.branchName}]]</label>
                </div>
            </div>
        </div>
        <div class="col-xs-12" style="margin-top: 20px;margin-left: 2%;margin-right: 2%;">
            <div class="form-inline">
                <div class="form-group">
                    <label for="select-prodNo">生产令编号</label>
                    <select id="select-prodNo" class="form-control" style="width: 140px;text-align: center"></select>
                </div>
                <div class="form-group">
                    <label for="select-special">类型</label>
                    <select id="select-special" class="form-control select2" style="width: 140px;text-align: center"></select>
                </div>
                <div class="form-group">
                    <label for="select-part">部分</label>
                    <select id="select-part" class="form-control select2" style="width: 200px;text-align: center"></select>
                </div>
                <div class="form-group">
                    <label for="select-drawNo">图号</label>
                    <select id="select-drawNo" class="form-control select2" style="width: 300px;text-align: center"></select>
                </div>
            </div>
        </div>
        <div class="col-xs-12" style="margin-top: 20px;margin-left: 2%;margin-right: 2%;">
            <form id="queryForm" onsubmit="return false;">
                <div class="form-inline">
                    <div class="form-group">
                        <label for="query-barCode">二维码区域</label>
                        <input id="query-barCode" type="text" name="barCode" class="form-control"
                               placeholder="请扫描装配派工单二维码"
                               style="width: 550px;margin-right: 10px;" autocomplete="off" onkeyup="loadData()">
                        <button type="button" class="btn btn-success" id="btn-search" style="margin-bottom: 0px;">
                            <span class="Bold">搜索</span>
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <hr style="margin-left: 2%;margin-right: 2%;margin-top: 30px;">
    <div class="row">
        <div class="col-xs-12">
            <div style="margin-top: 15px;margin-left: 1%;margin-right: 1%;">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>

</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let table;

    /** 合格模态框 */
    function openSucess(id) {
        $("#sucSheetId").val(id);
        js.modal.open("modal-success");
    }

    /** 驳回模态框 */
    function openReject(id) {
        $("#rejSheetId").val(id);
        js.modal.open("modal-reject");
    }

    /** 报废模态框 */
    function openScrap(id) {
        $("#scrSheetId").val(id);
        js.modal.open("modal-scrap");
    }

    /**
     * 检测回车
     */
    function loadData() {
        let ev = window.event || e;
        let keyCode = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
        if (keyCode == 13) {
            js.table.refresh(table);
        }
    }

    $(function () {

        /** 生产令号下拉选 */
        js.post({
            url: ctx + "assemble/apply/getRecProdNoList",
            data: {},
            success: function (result) {
                $("#select-prodNo").html('').select2();
                let select2Html = '<option value=""></option>';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                $("#select-prodNo").html(select2Html).select2();
            }
        });
        /** 项目编号下拉框选中 */
        $("#select-prodNo").on("select2:select", function () {
            getSpecials();
        });
        /** 类型下拉框选中 */
        $("#select-special").on("select2:select", function () {
            getParts();
        });
        /** 部分下拉框选中 */
        $("#select-part").on("select2:select", function () {
            getDrawNos();
        });
        /** 图号下拉框选中 */
        $("#select-drawNo").on("select2:select", function () {
            if ($("#select-prodNo").val() != null && $("#select-prodNo").val() != '' && $("#select-drawNo").val() != null && $("#select-drawNo").val() != '' && $("#select-part").val() != null && $("#select-part").val() != '') {
                $("#query-barCode").val($("#select-prodNo").val() + '|' + $("#select-drawNo").val() + '|' + $("#select-part").val() + '|2');
            } else {
                $("#query-barCode").val('');
            }
            js.table.refresh(table);
        });

        $('#bootstrap-table').bootstrapTable('destroy');//这里必须要添加这个销毁，否则新增、修改、查看的切换可编辑列表中的数据可能加载出现问题。
        /** 表单数据绑定 */
        table = js.table.init({
            url: ctx + "assemble/testing/testList",
            showExport: false,
            pagination: false,
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: "项目编码", field: 'proCode'},
                {title: "项目名称", field: 'proName'},
                {title: "类型", field: 'special'},
                {title: "部分", field: 'part'},
                {title: "装配图号", field: 'drawNo'},
                {title: "装配名称", field: 'drawName'},
                {title: "装配路线", field: 'route'},
                {title: "装配数量", field: 'drawNum'},
                {title: "申请质检数量", field: 'num'},
                {title: "申请人", field: 'applyName'},
                {title: "申请部门", field: 'applyDept'},
                {
                    title: "操作", width: '150',
                    formatter: function (value, row, index) {
                        let actions = [];
                        if (hasP('asseOrd:test:success')) {
                            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="openSucess(' + row.id + ')"> 合格</a>');
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="openReject(' + row.id + ')"> 不合格</a>');
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /** 点击查询 */
        $("#btn-search").click(function () {

            /** 校验二维码有效性 */
            var mm = $("#query-barCode").val().split("|");
            if (mm.length != 4) {
                js.modal.warning("二维码无效，请核对后再次扫描");
                return;
            }
            if (mm[3] != 2) {
                js.modal.warning("二维码无效，请核对后再次扫描");
                return;
            }
            js.table.search(table);
        });

        /** 合格 */
        $("#btn-sucess").click(function () {

            if ($('#sucStatus option:selected').val() == "2") {
                js.modal.warning("请填写质检意见！");
                return;
            }
            js.validSubmit({
                formId: "successForm",
                url: ctx + "assemble/testing/success",
                data: new FormData($("#successForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-success");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        });

        /** 驳回 */
        $("#btn-reject").click(function () {
            js.validSubmit({
                formId: "rejectForm",
                url: ctx + "assemble/testing/reject",
                data: new FormData($("#rejectForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-reject");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        });

        /** 报废 */
        $("#btn-scrap").click(function () {
            js.validSubmit({
                formId: "scrapForm",
                url: ctx + "assemble/testing/reject",
                data: new FormData($("#scrapForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-scrap");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        });

    })

    /** 获取类型列表 */
    function getSpecials() {
        js.post({
            url: ctx + "assemble/apply/getRecProdNoSpecialList",
            data: {prodNo: $("#select-prodNo").val()},
            success: function (result) {
                $("#select-special").empty();
                let select2Html = '';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                if (result.data.length > 0) {
                    $("#select-special").html(select2Html).select2();
                    $("#select-special").val(result.data[0].id).select2();
                }
                getParts();
            }
        });
    }
    /** 获取部分列表 */
    function getParts() {
        js.post({
            url: ctx + "assemble/apply/getRecProdNoPartList",
            data: {
                prodNo: $("#select-prodNo").val(),
                special: $("#select-special").val()
            },
            success: function (result) {
                $("#select-part").html('').empty();
                let select2Html = '';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                if (result.data.length > 0) {
                    $("#select-part").html(select2Html).select2();
                    $("#select-part").val(result.data[0].id).select2();
                }
                getDrawNos();
            }
        });
    }
    /** 获取图号列表 */
    function getDrawNos() {
        js.post({
            url: ctx + "assemble/apply/getRecProdNoDrawNoList",
            data: {
                prodNo: $("#select-prodNo").val(),
                special: $("#select-special").val(),
                partId: $("#select-part").val()
            },
            success: function (result) {
                $("#select-drawNo").html('').empty();
                let select2Html = '';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                if (result.data.length > 0) {
                    $("#select-drawNo").html(select2Html).select2();
                    $("#select-drawNo").val(result.data[0].id).select2();
                }
                if ($("#select-prodNo").val() != null && $("#select-prodNo").val() != '' && $("#select-drawNo").val() != null && $("#select-drawNo").val() != '' && $("#select-part").val() != null && $("#select-part").val() != '') {
                    $("#query-barCode").val($("#select-prodNo").val() + '|' + $("#select-drawNo").val() + '|' + $("#select-part").val() + '|2');
                } else {
                    $("#query-barCode").val('');
                }
                js.table.refresh(table);
            }
        });
    }

</script>
</body>
</html>
